/**
 * Mock数据生成工具
 * 使用Mock.js生成文件展示模块需要的模拟数据
 */
import Mock from 'mockjs'

// 图片URL生成
const imagePlaceholders = [
  'https://picsum.photos/800/600',
  'https://picsum.photos/900/600',
  'https://picsum.photos/800/500',
  'https://picsum.photos/700/500',
  'https://picsum.photos/600/400',
  'https://picsum.photos/500/400',
  'https://picsum.photos/400/300',
  'https://picsum.photos/300/200'
]

// 文件类型配置
const fileTypes = {
  image: {
    extensions: ['jpg', 'png', 'webp', 'svg', 'gif', 'bmp', 'psd', 'sketch', 'ai'],
    prefixes: ['图片', '截图', '照片', '设计稿', 'logo', '背景', '封面', '原型', '界面']
  },
  video: {
    extensions: ['mp4', 'avi', 'mov', 'wmv', 'mkv', 'flv', 'webm', 'mpeg'],
    prefixes: ['视频', '录屏', '宣传片', '教程', '演示', '直播回放', '会议记录', '产品介绍']
  },
  audio: {
    extensions: ['mp3', 'wav', 'ogg', 'flac', 'm4a', 'aac', 'wma'],
    prefixes: ['音频', '录音', '语音', '音乐', '背景音', '音效', '配音', '会议录音']
  },
  document: {
    extensions: ['pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'txt', 'md', 'json', 'csv'],
    prefixes: ['文档', '报告', '方案', '计划', '总结', '协议', '合同', '规范', '说明书']
  },
  archive: {
    extensions: ['zip', 'rar', '7z', 'tar', 'gz'],
    prefixes: ['压缩包', '备份', '资料', '素材', '源码', '配置', '依赖', '数据']
  }
}

// 文件大小范围（字节）
const fileSizeRanges = {
  image: [500000, 15000000],
  video: [5000000, 500000000],
  audio: [1000000, 50000000],
  document: [100000, 10000000],
  archive: [1000000, 100000000]
}

// 联系人列表
export const generateContacts = () => {
  return [
    { id: '1', name: '张三', avatar: '张', fileCount: 18 },
    { id: '2', name: '李四', avatar: '李', fileCount: 14 },
    { id: '3', name: '王五', avatar: '王', fileCount: 12 },
    { id: '4', name: '赵六', avatar: '赵', fileCount: 10 },
    { id: '5', name: '钱七', avatar: '钱', fileCount: 8 },
    { id: '6', name: '孙八', avatar: '孙', fileCount: 5 }
  ]
}

// 生成随机文件名
const generateFileName = (type: string) => {
  const typeConfig = fileTypes[type as keyof typeof fileTypes]
  const prefix = typeConfig.prefixes[Math.floor(Math.random() * typeConfig.prefixes.length)]
  const extension = typeConfig.extensions[Math.floor(Math.random() * typeConfig.extensions.length)]
  const random = Mock.Random.word(3, 8)

  return `${prefix}_${random}.${extension}`
}

// 生成随机文件大小
const generateFileSize = (type: string) => {
  const [min, max] = fileSizeRanges[type as keyof typeof fileSizeRanges]
  return Math.floor(min + Math.random() * (max - min))
}

// 为图片类型生成预览URL
const generatePreviewUrl = (type: string) => {
  if (type === 'image') {
    // 随机选择一个图片URL
    return imagePlaceholders[Math.floor(Math.random() * imagePlaceholders.length)]
  }
  return null
}

// 获取日期分布
const getDateDistribution = () => {
  const today = new Date()
  const yesterday = new Date(today)
  yesterday.setDate(yesterday.getDate() - 1)

  const dates = []

  // 今天和昨天
  dates.push(today, yesterday)

  // 最近一周（不包括今天和昨天）
  for (let i = 2; i < 7; i++) {
    const date = new Date(today)
    date.setDate(today.getDate() - i)
    dates.push(date)
  }

  // 最近一个月（再增加一些日期）
  for (let i = 7; i < 30; i += 3) {
    const date = new Date(today)
    date.setDate(today.getDate() - i)
    dates.push(date)
  }

  // 更早的一些日期
  for (let i = 1; i <= 5; i++) {
    const date = new Date(today)
    date.setMonth(today.getMonth() - i)
    dates.push(date)
  }

  return dates
}

// 生成指定数量的模拟文件数据
export const generateMockFiles = (count = 100) => {
  const contacts = generateContacts()
  const dates = getDateDistribution()
  const files = []
  const types = Object.keys(fileTypes)

  for (let i = 1; i <= count; i++) {
    const type = types[Math.floor(Math.random() * types.length)]
    const contact = contacts[Math.floor(Math.random() * contacts.length)]
    const date = dates[Math.floor(Math.random() * dates.length)]

    files.push({
      id: String(i),
      name: generateFileName(type),
      type,
      size: generateFileSize(type),
      date,
      sender: contact.name,
      previewUrl: generatePreviewUrl(type) // 添加预览URL
    })
  }

  return files
}

// 为特定联系人生成模拟文件
export const generateFilesForContact = (contactName: string, count = 20) => {
  const dates = getDateDistribution()
  const files = []
  const types = Object.keys(fileTypes)

  for (let i = 1; i <= count; i++) {
    const type = types[Math.floor(Math.random() * types.length)]
    const date = dates[Math.floor(Math.random() * dates.length)]

    files.push({
      id: `${contactName}-${i}`,
      name: generateFileName(type),
      type,
      size: generateFileSize(type),
      date,
      sender: contactName,
      previewUrl: generatePreviewUrl(type) // 添加预览URL
    })
  }

  return files
}

// 默认导出
export default {
  generateContacts,
  generateMockFiles,
  generateFilesForContact
}
